import 'package:yysk_app/model/address/address_node.dart';
import 'package:yysk_app/model/blind_model/list_by_blindId_count_model.dart';
import 'package:yysk_app/model/blind_model/list_by_blindId_model.dart';
import 'package:yysk_app/model/blind_model/list_by_blindId_record_model.dart';
import 'package:yysk_app/model/blind_model/my_blind_list_model.dart';
import 'package:yysk_app/model/blind_model/open_blind_model.dart';
import 'package:yysk_app/model/collection_model/first_publish_buy_order_model.dart';
import 'package:yysk_app/model/collection_model/first_publish_detail_model.dart';
import 'package:yysk_app/model/collection_model/first_publish_get_time_model.dart';
import 'package:yysk_app/model/collection_model/first_publish_model.dart';
import 'package:yysk_app/model/collection_model/get_qualification_list_model.dart';
import 'package:yysk_app/model/collection_model/home_first_detail_model.dart';
import 'package:yysk_app/model/collection_model/home_first_list_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_detail_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_id_head_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_id_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_model.dart';
import 'package:yysk_app/model/common_model/banner_list_model.dart';
import 'package:yysk_app/model/common_model/customer_service_qa_list_model.dart';
import 'package:yysk_app/model/common_model/get_certification_type_model.dart';
import 'package:yysk_app/model/common_model/get_upload_key_model.dart';
import 'package:yysk_app/model/common_model/list_handbook_model.dart';
import 'package:yysk_app/model/face_model/certification_result_model.dart';
import 'package:yysk_app/model/face_model/face_result_model.dart';
import 'package:yysk_app/model/common_model/mine_points_detail_model.dart';
import 'package:yysk_app/model/common_model/login_slider_model.dart';
import 'package:yysk_app/model/common_model/my_fans_model.dart';
import 'package:yysk_app/model/common_model/notice_label_list.dart';
import 'package:yysk_app/model/common_model/notice_list_model.dart';
import 'package:yysk_app/model/common_model/resp_user_model.dart';
import 'package:yysk_app/model/common_model/server_info_model.dart';
import 'package:yysk_app/model/common_model/static_info_model.dart';
import 'package:yysk_app/model/common_model/user_info_model.dart';
import 'package:yysk_app/model/market_model/market_batch_sale_list_model.dart';
import 'package:yysk_app/model/market_model/market_batch_sale_list_order_model.dart';
import 'package:yysk_app/model/market_model/market_batch_sale_my_list_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_detail_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_dynamic_list_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_list_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_time_model.dart';
import 'package:yysk_app/model/market_model/rob_list_model.dart';

import 'package:yysk_app/model/my_collection_model.dart';
import 'package:yysk_app/model/order_model/get_pay_token_model.dart';
import 'package:yysk_app/model/order_model/order_incubation_detail_model.dart';
import 'package:yysk_app/model/order_model/order_buy_incubation_list_model.dart';
import 'package:yysk_app/model/order_model/order_sale_list_model.dart';
import 'package:yysk_app/model/order_model/order_detail_model.dart';
import 'package:yysk_app/model/order_model/order_list_model.dart';

import 'package:yysk_app/model/pay_model/access_info_query_model.dart';
import 'package:yysk_app/model/pay_model/bank_card_bind_model.dart';
import 'package:yysk_app/model/pay_model/bank_card_confirm_bind_model.dart';
import 'package:yysk_app/model/pay_model/bank_card_list_model.dart';
import 'package:yysk_app/model/pay_model/bank_card_unbind_model.dart';
import 'package:yysk_app/model/pay_model/bill_page_model.dart';
import 'package:yysk_app/model/pay_model/calc_withdraw_amount_model.dart';
import 'package:yysk_app/model/pay_model/cashier_route_query_model.dart';
import 'package:yysk_app/model/pay_model/list_bank_branch_hf_model.dart';
import 'package:yysk_app/model/pay_model/list_bank_hf_model.dart';
import 'package:yysk_app/model/pay_model/open_wallet_info_model.dart';
import 'package:yysk_app/model/pay_model/page_with_draw_list_model.dart';
import 'package:yysk_app/model/pay_model/pay_token_apply_model.dart';
import 'package:yysk_app/model/pay_model/cashier_info_query_model.dart';
import 'package:yysk_app/model/pay_model/payment_deposit_model.dart';
import 'package:yysk_app/model/pay_model/payment_order_pay_confirm_model.dart';
import 'package:yysk_app/model/pay_model/payment_order_pay_model.dart';
import 'package:yysk_app/model/pay_model/payment_order_pay_query_model.dart';
import 'package:yysk_app/model/pay_model/payment_query_withdraw_rule_model.dart';
import 'package:yysk_app/model/pay_model/payment_withdraw_model.dart';
import 'package:yysk_app/model/pay_model/payment_withdraw_query_model.dart';
import 'package:yysk_app/model/pay_model/plat_wallet_flow_model.dart';
import 'package:yysk_app/model/pay_model/query_rechange_rrices_model.dart';
import 'package:yysk_app/model/pay_model/user_list_pay_certification_model.dart';
import 'package:yysk_app/model/pay_model/user_register_apply_model.dart';
import 'package:yysk_app/model/pay_model/user_register_apply_url_model.dart';
import 'package:yysk_app/model/pay_model/user_register_detail_query_model.dart';
import 'package:yysk_app/model/pay_model/user_register_query_model.dart';
import 'package:yysk_app/model/pay_model/wallet_reset_password_apply_model.dart';
import 'package:yysk_app/model/search/mall_goods_spu_es_model.dart';
import 'package:yysk_app/model/user_friend/recommend_user_info_model.dart';
import 'package:yysk_app/model/user_friend/user_friend_list_model.dart';
import 'package:yysk_app/model/user_friend/user_friend_reward_list_model.dart';
import 'package:yysk_app/model/user_group/user_union_detail_model.dart';
import 'package:yysk_app/model/user_group/user_union_member_list_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_count_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_history_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_result_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_send_model.dart';
import 'package:yysk_app/model/user_prop_model/my_prop_list_model.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/model/common_model/dao_page_model.dart';
import 'package:yysk_app/model/common_model/mine_points_model.dart';
import 'package:yysk_app/widget/region_pick_widget.dart';

import '../pay_model/user_pay_zhifu_url.dart';

JsonConvert jsonConvert = JsonConvert();

typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
typedef JsonListFunction<T> = T Function(Map<String, dynamic> json);
typedef EnumConvertFuntion<T> = T Function(dynamic value);
typedef ConverExceptionHandler =
    void Function(Object error, StackTrace stackTrace);

class JsonConvert {
  static ConverExceptionHandler? onError;
  JsonConvertClassCollection convertFunctMap = JsonConvertClassCollection();

  static M? fromJsonAsT<M>(dynamic json) {
    if (json is M) {
      return json;
    }
    if (json is List) {
      return _getListChlidType<M>(
        json.map((dynamic e) => e as Map<String, dynamic>).toList(),
      );
    } else {
      return jsonConvert.convert<M>(json);
    }
  }

  static M? _getListChlidType<M>(List<Map<String, dynamic>> list) {
    return null;
  }

  T? convert<T>(value, {EnumConvertFuntion? enumConvert}) {
    if (value == null) return null;
    if (value is T) return value;
    try {
      return _asT<T>(value, enumConvert: enumConvert);
    } catch (e, stackTrace) {
      if (onError != null) {
        onError!(e, stackTrace);
      }
    }
    return null;
  }

  T? _asT<T extends Object?>(dynamic value, {EnumConvertFuntion? enumConvert}) {
    final String type = T.toString();
    final String valueString = value.toString();
    consoleLog('type ===>$type');
    if (enumConvert != null) {
      return enumConvert(valueString);
    } else if (type == 'String') {
      return valueString as T;
    } else if (type == 'int') {
      return int.parse(valueString) as T;
    } else if (type == 'double') {
      return double.parse(valueString) as T;
    } else if (type == 'bool') {
      if (valueString == 'true') {
        return true as T;
      } else {
        return false as T;
      }
    } else if (type == 'DateTime') {
      return DateTime.parse(valueString) as T;
    } else if (type == 'Map' || type.startsWith('Map<')) {
      return value as T;
    } else {
      if (convertFunctMap.containsKey(type)) {
        if (value == null) {
          return null;
        }

        var covertFunc = convertFunctMap[type]!;

        if (covertFunc is Map<String, dynamic>) {
          return covertFunc(value as Map<String, dynamic>) as T;
        } else {
          return covertFunc(Map<String, dynamic>.from(value)) as T;
        }
      } else {
        throw UnimplementedError('$type 未实现, 你检查你的implemented是否实现');
      }
    }
  }
}

class JsonConvertListCollection {
  Map<String, JsonListFunction> convertFunctMap = {
    (MyCollectionModel).toString(): MyCollectionModel.fromJson,
  };

  bool containsKey(String type) {
    return convertFunctMap.containsKey(type);
  }

  JsonListFunction? operator [](String key) {
    return convertFunctMap[key];
  }
}

class JsonConvertClassCollection {
  Map<String, JsonConvertFunction> convertFunctMap = {
    (FirstPublishModel).toString(): FirstPublishModel.fromJson, //首发列表
    (FirstPublishDetailModel).toString():
        FirstPublishDetailModel.fromJson, //首发详情
    (UserInfoModel).toString(): UserInfoModel.fromJson, //用户信息RespUserModel
    (RespUserModel).toString(): RespUserModel.fromJson, //登录
    (BannerListModel).toString(): BannerListModel.fromJson, //banner 模型
    (NoticeLabelList).toString(): NoticeLabelList.fromJson, //公告laber模型
    (NoticeListModel).toString(): NoticeListModel.fromJson, //公告模型模型
    (UserPropsModel).toString(): UserPropsModel.fromJson, //我的道具
    (ListByPropIdModel).toString(): ListByPropIdModel.fromJson, //用户下某个道具列表
    (ListByPropIdCountModel).toString():
        ListByPropIdCountModel.fromJson, //当前用户下某个道具数量
    (ListByPropIdHistoryModel).toString():
        ListByPropIdHistoryModel.fromJson, //历史道具
    (MyCollectionListModel).toString(): MyCollectionListModel.fromJson, //我的藏品
    (MyCollectionListByCollectionIdModel).toString():
        MyCollectionListByCollectionIdModel.fromJson, //我的单个列表藏品
    (MyCollectionListByCollectionIdHeadModel).toString():
        MyCollectionListByCollectionIdHeadModel.fromJson, //我的单个列表藏品
    (MyCollectionListByCollectionDetailModel).toString():
        MyCollectionListByCollectionDetailModel.fromJson, //我的单个列表藏品详情
    (OrderListModel).toString(): OrderListModel.fromJson, //藏品订单列表
    (OrderBuyIncubationListModel).toString():
        OrderBuyIncubationListModel.fromJson, //购买孵化订单列表
    (OrderDetailModel).toString(): OrderDetailModel.fromJson, //订单详情
    (MinePointListModel).toString(): MinePointListModel.fromJson, //积分列表
    (MinePointDetailListModel).toString():
        MinePointDetailListModel.fromJson, //积分
    (DaoPageModel).toString(): DaoPageModel.fromJson, //dao列表
    (MyBlindListModel).toString(): MyBlindListModel.fromJson, //我的心愿盒列表
    (GetCertificationTypeModel).toString():
        GetCertificationTypeModel
            .fromJson, //获取实名认证状态-脱敏FirstPublishGetTimeModel
    (CertificationResultModel).toString():
        CertificationResultModel.fromJson, //实名认证结果
    (FaceResultModel).toString(): FaceResultModel.fromJson, //人脸识别结果
    (FirstPublishGetTimeModel).toString():
        FirstPublishGetTimeModel.fromJson, //获取首发时间
    (ServerInfoModel).toString():
        ServerInfoModel.fromJson, //ServerInfoModel 客服信息
    (StaticInfoModel).toString():
        StaticInfoModel.fromJson, //StaticInfoModel 客服信息
    //支付模块模型，后续需要单独切出来--支付中台开始
    (PayTokenApplyModel).toString():
        PayTokenApplyModel.fromJson, //唯一user请求获取支付凭证x-token
    //前台接口权限接口
    (AccessInfoQueryModel).toString():
        AccessInfoQueryModel.fromJson, // 获取支付中台token
    //支付接口模块
    (CalcWithdrawAmountModel).toString():
        CalcWithdrawAmountModel.fromJson, // 计算到账金额 模型
    (PaymentDepositModel).toString(): PaymentDepositModel.fromJson, //充值 模型
    (PaymentOrderPayModel).toString(): PaymentOrderPayModel.fromJson, //订单支付 模型
    (PaymentOrderPayConfirmModel).toString():
        PaymentOrderPayConfirmModel.fromJson, //订单支付确认。汇付支付需要 模型
    (PaymentOrderPayQueryModel).toString():
        PaymentOrderPayQueryModel.fromJson, //订单支付查询 模型
    (PaymentQueryWithdrawRuleModel).toString():
        PaymentQueryWithdrawRuleModel.fromJson, //提现规则查询 模型
    (PaymentWithdrawModel).toString():
        PaymentWithdrawModel.fromJson, //提现(此接口需根据三方支付确定，有些需要申请+确认)
    (PaymentWithdrawQueryModel).toString():
        PaymentWithdrawQueryModel.fromJson, //提现结果查询 模型
    //收银台接口模块
    (CashierInfoQueryModel).toString():
        CashierInfoQueryModel.fromJson, //获取订单信息 模型
    (CashierRouteQueryModel).toString():
        CashierRouteQueryModel.fromJson, //获取方式列表 模型
    //用户开户模块接口模块
    (UserListPayCertificationModel).toString():
        CashierRouteQueryModel.fromJson, // 获取开户列表 模型
    (UserRegisterApplyModel).toString():
        UserRegisterApplyModel.fromJson, //用户-企业-个体工商户 开户申请 模型
    (OpenWalletInfoModel).toString(): //OpenWalletInfoModel
        OpenWalletInfoModel.fromJson,
    (UserRegisterApplyUrlModel).toString():
        UserRegisterApplyUrlModel.fromJson, //开户申请：页面
    (UserRegisterDetailQueryModel).toString():
        UserRegisterDetailQueryModel.fromJson, //开户详细信息查询
    (UserRegisterQueryModel).toString():
        UserRegisterQueryModel.fromJson, //开户状态查询
    //用户绑卡接口模块
    (BankCardBindModel).toString():
        BankCardBindModel.fromJson, // 支付渠道绑定银行卡，返回绑卡id
    (BankCardConfirmBindModel).toString():
        BankCardConfirmBindModel.fromJson, //确认绑定银行卡
    (BankCardListModel).toString(): BankCardListModel.fromJson, //获取支付渠道银行卡列表
    (BankCardUnbindModel).toString(): BankCardUnbindModel.fromJson, //解绑银行卡
    //用户钱包 模块
    (WalletResetPasswordApplyModel).toString():
        WalletResetPasswordApplyModel.fromJson, // 设置-重置交易密码申请
    //账单 接口模块
    (BillPageModel).toString(): BillPageModel.fromJson, // 账单分页查询
    (HomeFirstListModel).toString():
        HomeFirstListModel.fromJson, //我的首发藏品列表HomeFirstDetailModel
    (HomeFirstDetailModel).toString():
        HomeFirstDetailModel.fromJson, //我的首发藏品，心愿盒，道具
    (MyFansModel).toString(): MyFansModel.fromJson, //我的粉丝
    (FirstPublishBuyOrderModel).toString():
        FirstPublishBuyOrderModel.fromJson, //首发创建订单
    (GetPayTokenModel).toString(): GetPayTokenModel.fromJson, //获取支付令牌
    (OrderSaleListModel).toString(): OrderSaleListModel.fromJson, //，心愿盒订单
    (ListByBlindIdCountModel).toString():
        ListByBlindIdCountModel.fromJson, //当前用户下某个心愿盒数量
    (ListByBlindIdModel).toString(): ListByBlindIdModel.fromJson, //单个心愿盒列表
    (ListByBlindIdRecordModel).toString():
        ListByBlindIdRecordModel.fromJson, //我的心愿盒记录列表
    (OrderIncubationDetailModel).toString():
        OrderIncubationDetailModel.fromJson, //心愿盒订单详情
    (LoginSliderModel).toString(): LoginSliderModel.fromJson, //获取滑块校验接口
    (OpenBlindModel).toString(): OpenBlindModel.fromJson, //开心愿盒//LevelModel
    (LevelModel).toString(): LevelModel.fromJson, //省市区
    (OpenBlindModel).toString(): OpenBlindModel.fromJson, //开盲盒//LevelModel
    (LevelModel).toString(): LevelModel.fromJson, //省市区
    (AddrList).toString(): AddrList.fromJson,

    (ListByPropIdSendModel).toString(): ListByPropIdSendModel.fromJson,
    (ListByPropIdResultModel).toString():
        ListByPropIdResultModel.fromJson, //列表盲盒 藏品 道具

    (ListByPropIdResultModel).toString():
        ListByPropIdResultModel.fromJson, //列表盲盒 藏品 道具
    (ListBankHfModel).toString(): ListBankHfModel.fromJson,
    (ListBankBranchHfModel).toString(): ListBankBranchHfModel.fromJson,
    (UserPayZhifuUrlModel).toString(): UserPayZhifuUrlModel.fromJson,
    (MallGoodsSpuEsModel).toString(): MallGoodsSpuEsModel.fromJson,
    (ListHandbookModel).toString(): ListHandbookModel.fromJson, //新人手册
    (UserUnionDetailModel).toString(): UserUnionDetailModel.fromJson, //新人手册
    (UnionMemberListModel).toString(): UnionMemberListModel.fromJson, //新人手册
    //获取资格信息
    (GetQualificationListModel).toString():
        GetQualificationListModel.fromJson, //获取资格信息
    (MarketIncubationTimeModel).toString():
        MarketIncubationTimeModel.fromJson, //获取孵化时间
    (MarketIncubationDetailModel).toString():
        MarketIncubationDetailModel.fromJson, //获取孵化配置详情
    (MarketIncubationListModel).toString():
        MarketIncubationListModel.fromJson, //孵化配置分页列表
    (UserFriendListModel).toString(): UserFriendListModel.fromJson, //好友列表
    (PlatWalletFlowModel).toString(): PlatWalletFlowModel.fromJson, //分页查询平台钱包流水
    (PathWithDrawListModel).toString(): PathWithDrawListModel.fromJson, //分页查询提现

    (MarketBatchSaleListModel).toString():
        MarketBatchSaleListModel.fromJson, //购买-获取孵化订单列表
    (MarketBatchSaleMyListModel).toString():
        MarketBatchSaleMyListModel.fromJson, //我的出售单列表
    (QueryRechangePricesModel).toString():
        QueryRechangePricesModel.fromJson, //获取余额充值金额列表
    (RecommendUserInfoModel).toString():
        RecommendUserInfoModel.fromJson, //查询邀请人信息
    (UserFriendRewardListModel).toString():
        UserFriendRewardListModel.fromJson, //分页查询好友奖励列表

    (RobListModel).toString(): RobListModel.fromJson, //智能购买
    (CustomerServiceListModel).toString():
        CustomerServiceListModel.fromJson, //qa列表
    (RobListModel).toString(): RobListModel.fromJson, //智能购买
    (MarketBatchSaleListOrderModel).toString():
        MarketBatchSaleListOrderModel.fromJson, //批量创建孵化订单
    (MarketIncubationDynamicListModel).toString():
        MarketIncubationDynamicListModel.fromJson, //获取孵化动态
            (GetUploadKeyModel).toString():
        GetUploadKeyModel.fromJson, //获取上传临时ak-sk信息
  };

  bool containsKey(String type) {
    return convertFunctMap.containsKey(type);
  }

  JsonConvertFunction? operator [](String key) {
    return convertFunctMap[key];
  }
}
